<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Alone extends CIC_A_Controller
{
	var $category_tree;
	function __construct()
	{
		parent::__construct();
		$this->init();
	}
	
	private function init()
	{
		$this->load->helper('ckeditor');
		$this->category_tree 	= $this->get_category_tree(5);
	}
	
	function index()
	{
		$this->admin_priv(__METHOD__);
		$data					= $this->get_alone();
		$data['category_tree']	= $this->category_tree;
		$this->load->view('admin/alone/index',$data);
	}
	
	
	function get_alone()
	{
		$where	= '';
		$and 	= '';
		$url = site_url('admin/alone/index?');
		$category_id = $this->input->get('category_id');
		$key_words	 = $this->input->get('key_words');
		if(! empty($category_id))
		{
			$where .= 'category_id = '.$category_id;
			$and	= ' and ';
			$url   .= 'category_id='.$category_id;
		}
		if(! empty($key_words))
		{
			$where .= $and."alone_title like '%$key_words%'";
			$url   .= '&key_words='.$key_words;
		}
		$data['page']	 = $this->page($url, $this->cic_model->get_total_rows('alone',$where), $this->per_page,TRUE);
		$data['alone'] 	 = $this->cic_model->get_all('alone',$where,array('alone_create_time','desc'),$this->per_page,$this->input->get('per_page'));
		return $data;
	}
	
	function content_add()
	{
		$data = array(
				'ckeditor_config'	=> $this->ckeditor_config('ckeditor'),
				'category_tree'		=> $this->category_tree,
		);
		$this->load->view('admin/alone/content_add',$data);
	}
	
	function content_edit()
	{
		$data = array(
				'ckeditor_config'	=> $this->ckeditor_config('ckeditor'),
				'category_tree'		=> $this->category_tree,
				'alone'			=> $this->cic_model->get_one('alone',array('alone_id'=>$this->uri->segment(4))),
		);
		$this->load->view('admin/alone/content_edit',$data);
	}
	function add_content()
	{
		$this->admin_priv(__METHOD__);
		$this->validate('alone', 'admin/alone/content_add');
		$data = array(
				'category_id'			=> $this->input->post('category_id'),
				'alone_title'			=> $this->input->post('alone_title',FALSE),
				'alone_content'			=> $this->input->post('alone_content',FALSE),			
				'alone_keywords'		=> $this->input->post('alone_keywords'),
				'alone_desc'			=> $this->input->post('alone_desc'),
				'alone_new_window'		=> $this->input->post('alone_new_window'),
				'alone_hits'			=> $this->get_config('ciccms/admin','sys','hits')?$this->get_config('ciccms/admin','sys','hits'):mt_rand(1,500),
				'alone_create_time'		=> time(),
				'alone_user_add'		=> $this->session->userdata('admin_user'),	
		);
		if($this->cic_model->insert('alone',$data))
		{
			$this->message->msg('添加成功！',site_url('admin/alone/index'));
		}else{
			$this->message->msg('添加失败！',site_url('admin/alone/content_add'));
		}
	}
	
	function edit_content()
	{
		$this->admin_priv(__METHOD__);
		$this->validate('alone', 'admin/alone/content_edit/'.$this->uri->segment(4));
		$data = array(
				'category_id'			=> $this->input->post('category_id'),
				'alone_title'			=> $this->input->post('alone_title',FALSE),
				'alone_content'		=> $this->input->post('alone_content',FALSE),
				'alone_attribute'		=> serialize($this->input->post('alone_attribute')),
				'alone_source'		=> $this->input->post('alone_source'),
				'alone_author'		=> $this->input->post('alone_author'),
				'alone_thumb_image'	=> $this->input->post('alone_thumb_image'),
				'alone_zhiding'		=> $this->input->post('alone_zhiding'),
				'alone_tuijian'		=> $this->input->post('alone_tuijian'),
				'alone_keywords'		=> $this->input->post('alone_keywords'),
				'alone_desc'			=> $this->input->post('alone_desc'),
				'alone_new_window'	=> $this->input->post('alone_new_window'),
		);
		if($this->cic_model->update('alone',$data,array('alone_id'=>$this->uri->segment(4))))
		{
			$this->message->msg('添加成功！',site_url('admin/alone/index'));
		}else{
			$this->message->msg('添加失败！',site_url('admin/alone/content_add'));
		}
	}
	
	function delete_alone()
	{
		$this->admin_priv(__METHOD__);
		if($this->cic_model->delete('alone',array('alone_id'=>$this->input->post('alone_id'))))
		{
			echo 1;
		}else{
			echo 0;
		}
	}
}